<?

namespace Admin;

use AdminController;
use Bcategory;
use Blog;
use Config;
use Input;
use Lang;
use MyModel;
use Redirect;
use Sentry;
use Str;
use Validator;
use View;

class NewsController extends AdminController
{
    public function getIndexCategory()
    {
        $data['categories'] = Bcategory::all();
        return View::make('backend/news/categories/index', compact('data'));
    }

    public function getCreateCategory()
    {
        if (!Sentry::getUser()->hasAnyAccess(['news', 'news.categories']))
            return View::make('backend/notallow');
        $data['category'] = new Bcategory();
        return View::make('backend/news/categories/create', compact('data'));
    }

    public function postCreateCategory()
    {
        if (!Sentry::getUser()->hasAnyAccess(['news', 'news.categories']))
            return View::make('backend/notallow');
        $rules = array('title' => 'required');
        $data = Input::get('data');
        $validation = Validator::make($data['Category'], $rules);
        if ($validation->fails())
            return Redirect::back()->withInput()->withErrors($validation);
        $category = new Bcategory();
        try {
            $data['Category']['slug'] = Str::slug($data['Category']['title']);
            $id = $category->saveItem($data['Category']);
        } catch (Exception $ex) {
            return Redirect::back()->withInput()->withErrors($ex);
        }
        return Redirect::route('update/category/news', $id)->with('success', '');
    }

    public function getEditCategory($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['news', 'news.categories']))
            return View::make('backend/notallow');
        if (is_null($category = Bcategory::find($id)))
            return Redirect::route('category/news')->with('error', '');
        $data['category'] = $category;
        return View::make('backend/news/categories/create', compact('data'));
    }

    public function getDeleteCategory($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['news', 'news.categories']))
            return View::make('backend/notallow');
        if (is_null($category = Bcategory::withTrashed()->find($id)))
            return Redirect::route('category/news')->with('error', '');
        $category->delete();
        return Redirect::route('category/news')->with('success', '');
    }

    public function getRestoreCategory($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['news', 'news.categories']))
            return View::make('backend/notallow');
        if (is_null($category = Bcategory::find($id)))
            return Redirect::route('category/news')->with('error', '');
        $category->restore();
        return Redirect::route('category/news')->with('success', '');
    }

    public function getIndex()
    {
        $data['appends']['k'] = Input::get('k', '');
        $data['appends']['withTrashed'] = Input::get('withTrashed', false);
        $data['appends']['onlyTrashed'] = Input::get('onlyTrashed', false);
        $data['appends']['category'] = Input::get('category', '');
        $blogs = new Blog();
        if ($data['appends']['withTrashed'])
            $blogs = $blogs->withTrashed();
        if ($data['appends']['onlyTrashed'])
            $blogs = $blogs->onlyTrashed();
        $myModel = new MyModel();
        $data['categories'] = $myModel->listAllItemsToArray('Bcategory', array('value' => 'id', 'display' => 'title'));
        $data['Blogs'] = $blogs->join('bcategories', 'bcategories.id', '=', 'blogs.ncategory_id')
            ->where(function ($where) use ($data) {
                if ($data['appends']['category'] != '') {
                    $where->where('ncategory_id', '=', $data['appends']['category']);
                }
                if ($data['appends']['k'] != '') {
                    $where->where('blogs.title', 'LIKE', '%' . $data['appends']['k'] . '%');
                }
            })
            ->select('blogs.id', 'blogs.title', 'blogs.created_at', 'bcategories.title as c_title')
            ->orderBy('id', 'DESC')->paginate()->appends($data['appends']);
        return View::make('backend/news/index', compact('data'));
    }

    public function getCreate()
    {
        if (!Sentry::getUser()->hasAnyAccess(['news', 'news.create']))
            return View::make('backend/notallow');
        $data['news'] = new Blog();
        $data['categories'] = Bcategory::ReturnToArray();
        return View::make('backend/news/create', compact('data'));
    }

    public function postCreate()
    {
        if (!Sentry::getUser()->hasAnyAccess(['news', 'news.create']))
            return View::make('backend/notallow');
        $data = Input::get('data');
        $rules = array('title' => 'required', 'content' => 'required');
        $validation = Validator::make($data['News'], $rules);
        if ($validation->fails())
            return Redirect::back()->withInput()->withErrors($validation);
        $blog = new Blog();
        $data['News']['slug'] = Str::slug($data['News']['title']);
        $id = $blog->saveItem($data['News']);
        return Redirect::route('update/news', $id)->with('success', '');

    }

    public function getEdit($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['news', 'news.create']))
            return View::make('backend/notallow');
        if (is_null($news = Blog::find($id)))
            return Redirect::route('news')->with('error', '');
        $data['news'] = $news;
        $data['categories'] = Bcategory::ReturnToArray();
        return View::make('backend/news/create', compact('data'));
    }

    public function getDelete($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['news', 'news.delete']))
            return View::make('backend/notallow');
        if (is_null($news = Blog::withTrashed()->find($id)))
            return Redirect::route('category/news')->with('error', '');
        $news->delete();
        return Redirect::route('category/news')->with('success', '');
    }

    public function getRestore($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['news', 'news.delete']))
            return View::make('backend/notallow');
        if (is_null($news = Blog::withTrashed()->find($id)))
            return Redirect::route('category/news')->with('error', '');
        $news->restore();
        return Redirect::route('category/news')->with('success', '');
    }
}

